package com.zhangh.summer.admin.support.spring;

import com.alibaba.druid.filter.logging.Log4j2Filter;
import com.zhangh.summer.admin.support.boot.common.AppConfigEnum;
import com.zhangh.summer.admin.support.boot.interfaces.OnAppConfigConditional;
import com.zhangh.summer.admin.support.ds.mysql.common.DataSourceSupport;
import com.zhangh.summer.admin.support.health.DataSourceHealth;
import com.zhangh.summer.admin.support.multipleds.DynamicDataSource;
import com.zhangh.summer.admin.support.multipleds.EnvHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@OnAppConfigConditional(AppConfigEnum.MYSQL_DATASOURCE_ENABLE)
@Slf4j
@MapperScan("com.duzhe.erp.support.ds.mysql.mapper")//设置个路径即可，防止警告
public  class DataSourceConfig {

   // private  Environment environment;
    @Resource
    private DataSource dataSource;

    public DataSourceConfig(){
        log.info("load datasource config");
    }

    @Bean(name="envHelper")
    public EnvHelper envHelper(){
        EnvHelper envHelper =new EnvHelper();
        return envHelper;
    }
    @Bean(name="dynamicDataSource")
    @Primary
    public DynamicDataSource dynamicDataSource(@Qualifier("envHelper") EnvHelper envHelper) throws Exception{
        DynamicDataSource dynamicDataSource =new DynamicDataSource();
        dynamicDataSource.setEnv(envHelper.getEnvironment());
        return dynamicDataSource;
    }

    /**
     * 根据数据源创建SqlSessionFactory
     */
    @Bean(name="sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(dataSource);// 指定数据源(这个必须有，否则报错)
        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mybatis/mapper/**/*.xml"));
        fb.setConfigLocation(new PathMatchingResourcePatternResolver().getResources("classpath*:/mybatis/mybatis.xml")[0]);
        return fb.getObject();
    }

    @Bean(name="dataSourceSupport")
    public DataSourceSupport dataSourceSupport(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        DataSourceSupport dss=new DataSourceSupport();
        dss.setSqlSessionFactory(sqlSessionFactory);
        return dss;
    }

    @Bean
    public String MybatisInterceptor(SqlSessionFactory sqlSessionFactory, @Qualifier("envHelper") EnvHelper envHelper) {
        String host=envHelper.getEnvironment().getProperty("ds.tidb.shard.host");
        if(StringUtils.isNotBlank(host)){
            //sqlSessionFactory.getConfiguration().addInterceptor(new TiDbShardInterceptor(dynamicDataSourceExpand));
        }
        //sqlSessionFactory.getConfiguration().addInterceptor(executorInterceptor);
        return "mybatisInterceptor";
    }

    @Bean(name="txManager")
    public DataSourceTransactionManager transactionManager() throws Exception {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name="dbHealthIndicator")//name必须是dbHealthIndicator 才可覆盖
    public DataSourceHealth dataSourceHealth(){
        //log.info(">>>dbHealthIndicator");
        DataSourceHealth dataSourceHealth =new DataSourceHealth();
        return dataSourceHealth;
    }
    @Bean
    public Log4j2Filter log4j2Filter(){
        Log4j2Filter log4j2Filter=new Log4j2Filter();
        log4j2Filter.setConnectionLogEnabled(false);
        log4j2Filter.setStatementLogEnabled(false);
        log4j2Filter.setResultSetLogEnabled(true);
        log4j2Filter.setStatementExecutableSqlLogEnable(true);
        return log4j2Filter;
    }


}
